/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MessagesReader.java
 *
 * Created on 11/03/2009, 20:49:22
 */
package br.unisc.pdaapp.ui.messages;

import br.unisc.pdaapp.constants.IPanels;
import br.unisc.pdaapp.meta.Message;
import br.unisc.pdaapp.meta.User;
import br.unisc.pdaapp.meta.ILoadableForm;
import br.unisc.pdaapp.ui.MoodleMobileFrame;
import br.unisc.pdaapp.webservice.client.WSHandler;
import br.unisc.pdaapp.xml.MoodleXmlReader;
import java.util.Vector;
import sun.misc.BASE64Decoder;

/**
 *
 * @author guilherme
 */
public class MessagesReaderPanel extends javax.swing.JPanel implements ILoadableForm
{

    private static MessagesReaderPanel instance;

    private Vector messages;

    private int index = -1;

    /** Creates new form MessagesReader */
    private MessagesReaderPanel()
    {
        initComponents();
    }

    public static MessagesReaderPanel getInstance()
    {
        if (instance == null)
        {
            instance = new MessagesReaderPanel();
        }

        return instance;
    }

    private void deleteMessage()
    {
        if (messages.size() > 0)
        {
            WSHandler wsHandler;
            try
            {
                wsHandler = new WSHandler();
                Message message = (Message) messages.get(index);
                int result = wsHandler.deleteMessage(message.getId());

                if (result == 1)
                {
                    messages.remove(index);
                    index--;
                    warningsLabel.setText("Mensagem deletada!");
                }
                else
                {
                    warningsLabel.setText("Mensagem não deletada!");
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    }

    private void loadMessages()
    {
        WSHandler wsHandler;
        try
        {
            wsHandler = new WSHandler();

            User user = MoodleMobileFrame.getInstance().getUser();
            byte[] result = wsHandler.getMessages(user.getUserName());

            BASE64Decoder based = new BASE64Decoder();

            String encoded = new String(result);
            byte[] decoded = based.decodeBuffer(encoded);
            String xml = new String(decoded);
            System.out.println("xml: " + xml);

            MoodleXmlReader moodleXmlReader = new MoodleXmlReader();
            moodleXmlReader.readXML(xml, MoodleXmlReader.MESSAGES, decoded);
            messages = moodleXmlReader.getResultData();

            index = 0;

            updateMessage();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public void loadForm()
    {
        loadMessages();
    }

    public void setHeader(String text)
    {
    }

    private void updateMessage()
    {
        if (messages.size() > 0)
        {
            warningsLabel.setText(" ");
            Message message = (Message) messages.get(index);

            fromLabel.setText(message.getUserFrom());
            actualMsgLabel.setText("" + (index + 1));
            totalMessagesLabel.setText("" + messages.size());
            messageTextArea.setText(message.getMessage());
        }
        else
        {
            warningsLabel.setText("Sem mensagens!");
            fromLabel.setText("");
            actualMsgLabel.setText("0");
            totalMessagesLabel.setText("0");
            messageTextArea.setText("Sem mensagens");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel3 = new javax.swing.JPanel();
        backButton1 = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        warningsLabel = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        prevButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        messagesLabel = new javax.swing.JLabel();
        actualMsgLabel = new javax.swing.JLabel();
        deLabel = new javax.swing.JLabel();
        totalMessagesLabel = new javax.swing.JLabel();
        fromDescLabel = new javax.swing.JLabel();
        fromLabel = new javax.swing.JLabel();
        deleteMesssageButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        messageTextArea = new javax.swing.JTextArea();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());

        jPanel3.setOpaque(false);
        jPanel3.setLayout(new java.awt.GridBagLayout());

        backButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/back.jpeg"))); // NOI18N
        backButton1.setBorder(null);
        backButton1.setOpaque(false);
        backButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 1);
        jPanel3.add(backButton1, gridBagConstraints);

        exitButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/close.jpeg"))); // NOI18N
        exitButton.setBorder(null);
        exitButton.setOpaque(false);
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel3.add(exitButton, gridBagConstraints);

        warningsLabel.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        warningsLabel.setForeground(new java.awt.Color(255, 0, 0));
        warningsLabel.setText(" ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        jPanel3.add(warningsLabel, gridBagConstraints);

        add(jPanel3, java.awt.BorderLayout.NORTH);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel2.setOpaque(false);
        jPanel2.setLayout(new java.awt.GridBagLayout());

        prevButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/left.jpeg"))); // NOI18N
        prevButton.setBorder(null);
        prevButton.setOpaque(false);
        prevButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prevButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 0);
        jPanel2.add(prevButton, gridBagConstraints);

        nextButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/right.JPG"))); // NOI18N
        nextButton.setBorder(null);
        nextButton.setOpaque(false);
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 0);
        jPanel2.add(nextButton, gridBagConstraints);

        refreshButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/refresh.jpeg"))); // NOI18N
        refreshButton.setBorder(null);
        refreshButton.setOpaque(false);
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 0);
        jPanel2.add(refreshButton, gridBagConstraints);

        messagesLabel.setFont(new java.awt.Font("Tahoma", 0, 10));
        messagesLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/emai.jpeg"))); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(3, 2, 0, 3);
        jPanel2.add(messagesLabel, gridBagConstraints);

        actualMsgLabel.setFont(new java.awt.Font("Tahoma", 0, 10));
        actualMsgLabel.setText("1");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 3);
        jPanel2.add(actualMsgLabel, gridBagConstraints);

        deLabel.setFont(new java.awt.Font("Tahoma", 0, 10));
        deLabel.setText("de");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(6, 2, 0, 2);
        jPanel2.add(deLabel, gridBagConstraints);

        totalMessagesLabel.setFont(new java.awt.Font("Tahoma", 0, 10));
        totalMessagesLabel.setText("12");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 7;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(6, 2, 0, 0);
        jPanel2.add(totalMessagesLabel, gridBagConstraints);

        fromDescLabel.setFont(new java.awt.Font("Tahoma", 0, 10));
        fromDescLabel.setText("De:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(6, 2, 0, 3);
        jPanel2.add(fromDescLabel, gridBagConstraints);

        fromLabel.setFont(new java.awt.Font("Tahoma", 0, 10));
        fromLabel.setText("User");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 9;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(6, 2, 0, 2);
        jPanel2.add(fromLabel, gridBagConstraints);

        deleteMesssageButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/del.jpeg"))); // NOI18N
        deleteMesssageButton.setBorder(null);
        deleteMesssageButton.setOpaque(false);
        deleteMesssageButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteMesssageButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 1);
        jPanel2.add(deleteMesssageButton, gridBagConstraints);

        jPanel1.add(jPanel2, java.awt.BorderLayout.NORTH);

        messageTextArea.setColumns(20);
        messageTextArea.setEditable(false);
        messageTextArea.setRows(5);
        jScrollPane2.setViewportView(messageTextArea);

        jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        add(jPanel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void prevButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_prevButtonActionPerformed
    {//GEN-HEADEREND:event_prevButtonActionPerformed
        if (index > 0)
        {
            index--;
            updateMessage();
        }
    }//GEN-LAST:event_prevButtonActionPerformed

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_nextButtonActionPerformed
    {//GEN-HEADEREND:event_nextButtonActionPerformed
        if (index < messages.size() - 1)
        {
            index++;
            updateMessage();
        }
    }//GEN-LAST:event_nextButtonActionPerformed

    private void backButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_backButton1ActionPerformed
    {//GEN-HEADEREND:event_backButton1ActionPerformed
        MoodleMobileFrame.getInstance().switchPanel(IPanels.MESSAGES_PANE);
}//GEN-LAST:event_backButton1ActionPerformed

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_exitButtonActionPerformed
    {//GEN-HEADEREND:event_exitButtonActionPerformed
        MoodleMobileFrame.getInstance().quit(this);
}//GEN-LAST:event_exitButtonActionPerformed

    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_refreshButtonActionPerformed
    {//GEN-HEADEREND:event_refreshButtonActionPerformed
        loadMessages();
}//GEN-LAST:event_refreshButtonActionPerformed

    private void deleteMesssageButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_deleteMesssageButtonActionPerformed
    {//GEN-HEADEREND:event_deleteMesssageButtonActionPerformed
        deleteMessage();
}//GEN-LAST:event_deleteMesssageButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel actualMsgLabel;
    private javax.swing.JButton backButton1;
    private javax.swing.JLabel deLabel;
    private javax.swing.JButton deleteMesssageButton;
    private javax.swing.JButton exitButton;
    private javax.swing.JLabel fromDescLabel;
    private javax.swing.JLabel fromLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea messageTextArea;
    private javax.swing.JLabel messagesLabel;
    private javax.swing.JButton nextButton;
    private javax.swing.JButton prevButton;
    private javax.swing.JButton refreshButton;
    private javax.swing.JLabel totalMessagesLabel;
    private javax.swing.JLabel warningsLabel;
    // End of variables declaration//GEN-END:variables

}
